package game;

import game.Config.Config;
import game.Config.Config.GameInput;
import game.Screens.MenuScreen;
import game.Screens.Screen;

import java.util.Stack;

import org.newdawn.slick.BasicGame;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.AppGameContainer;

/**
 * This class is the main class of the Tower Defense Game,
 * and thus contains the main method.
 * @author Sebastian Isheden and Victor Beer
 *
 */
public class Game extends BasicGame {

	public final static Stack<Screen> screenStack = new Stack<Screen>();
	public Game(String title) {
		super(title);
	}

	/**
	 * Starts the Tower Defense Game.
	 */
	public static void main(String[] args) {
		try{
			AppGameContainer app = new AppGameContainer(new Game("Tower Defense Game"),Config.SCREEN_WIDTH,Config.SCREEN_HEIGHT,false);
			app.start();
		}catch(SlickException e){
			e.printStackTrace();
		}
		
	}
	/**
	 * This method is called after each call to update.
	 * Peeks on the topmost screen and calls its draw method.
	 */
	@Override
	public void render(GameContainer gc, Graphics graphic) throws SlickException {
		screenStack.peek().draw(graphic);
	}
	/**
	 * This method is called when the game is created.
	 * Initializes the configurations for the game
	 * and starts the first screen, the MenuScreen.
	 */
	@Override
	public void init(GameContainer arg0) throws SlickException {
		Config.initialize();
		screenStack.push(new MenuScreen());
		screenStack.peek().begin();
		arg0.setShowFPS(false);
	}
	/**
	 * This is the update method of the game.
	 * The integer delta determines how long ago
	 * the last update occured.
	 * This method updates the Keyboard(reading any input)
	 * and peeks on the topmost-screen, calling its update method.
	 */
	@Override
	public void update(GameContainer gc, int delta) throws SlickException {
		Keyboard.update(this, gc);
		if(screenStack.size() == 0){
			System.exit(0);
		}
		
		screenStack.peek().update(gc, delta);
		
	}
	/**
	 * This method is called when input has been detected
	 * to be pressed.
	 * 
	 * @param input	The input detected.
	 */
	public void inputPressed(GameInput input){
		screenStack.peek().inputPressed(input);
	}
	/**
	 * This method is called when input has been detected
	 * to be down.
	 * @param input The input detected.
	 */
	public void inputDown(GameInput input){
		screenStack.peek().inputDown(input);
	}
	/**
	 * Pushes a new Screen on the stack and invokes its begin method.
	 * @param screen The screen we want to push.
	 */
	public static void pushNewScreen(Screen screen){
		screenStack.push(screen);
		screen.begin();
	}

}
